﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace syc.Jidong
{
    public partial class 物料品种录入 : System.Web.UI.Page
    {
        protected string Notice = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            int id = 0;
            if (!this.IsPostBack)
            {
                if (Request.QueryString["ID"] != null)
                {
                    int.TryParse(Request.QueryString["ID"], out id);
                }

                if (id > 0)
                {
                    this.SqlDataSource1.SelectParameters["ID"].DefaultValue = id.ToString();
                    this.SqlDataSource1.Select(DataSourceSelectArguments.Empty);
                }
                else
                {
                    string s = Request.QueryString["fid"];
                    if (s == null)
                    {
                        s = "";
                    }
                    int fid = 0;
                    int.TryParse(s, out fid);
                    this.FID.Text = fid.ToString("D7");
                }

            }
        }

        protected void Button_Save_Click(object sender, EventArgs e)
        {

            int id;
            this.Notice = "“编码”录入有误，请录入一个 001 ～ 999 的数字";
            if (!int.TryParse(this.BianMa.Text, out id))
            {
                return;
            }
            if (id < 1 || id > 999)
            {
                return;
            }
            this.Notice = "";

            if (this.PinMing.Text == "")
            {
                this.Notice = "“品名”必须录入";
                return;
            }
            int qz;
            if (!int.TryParse(this.QuanZhong.Text, out qz))
            {
                this.QuanZhong.Text = "0";
            }
            decimal dj;
            if (!decimal.TryParse(this.DanJia.Text, out dj))
            {
                this.Notice = "“单价”录入有误";
                return;
            }
            this.Label_ID.Text = (int.Parse(this.FID.Text) * 1000 + id).ToString();

            if (this.BianMa.Enabled)
            {
                try
                {
                    if (this.SqlDataSource1.Insert() > 0)
                    {
                        this.Notice = "新增品种成功";
                    }
                }
                catch (Exception ex)
                {
                    this.Notice = ex.Message;
                    this.Notice = "新增品种失败，编码是否重复";
                }
            }
            else
            {
                if (this.SqlDataSource1.Update() > 0)
                {
                    this.Notice = "保存成功";
                }
            }

        }

        protected void SqlDataSource1_Selected(object sender, SqlDataSourceStatusEventArgs e)
        {
            if (!Convert.IsDBNull(e.Command.Parameters[1].Value))
            {
                int id = (int)e.Command.Parameters[0].Value;

                this.FID.Text = (id / 1000).ToString("D7");
                this.BianMa.Text = (id % 1000).ToString("D3");
                this.BianMa.Enabled = false;
                this.BianMa.BackColor = System.Drawing.Color.FromName("#eeeeee");


                this.PinMing.Text = (string)e.Command.Parameters[1].Value;
                if (!Convert.IsDBNull(e.Command.Parameters[2].Value))
                {
                    this.GuiGe.Text = (string)e.Command.Parameters[2].Value;
                }
                if (!Convert.IsDBNull(e.Command.Parameters[3].Value))
                {
                    this.DanWei.Text = (string)e.Command.Parameters[3].Value;
                }
                if (!Convert.IsDBNull(e.Command.Parameters[4].Value))
                {
                    this.DanJia.Text = ((double)e.Command.Parameters[4].Value).ToString();
                }
                 if (!Convert.IsDBNull(e.Command.Parameters[5].Value))
                {
                    this.JianMa.Text = (string)e.Command.Parameters[5].Value;
                }
                if (!Convert.IsDBNull(e.Command.Parameters[6].Value))
                {
                    this.QuanZhong.Text = ((int)e.Command.Parameters[6].Value).ToString();
                }
                this.DaZong.Checked = (bool)e.Command.Parameters[7].Value;
            }
            else
            {
                this.Notice="物料品种信息读取失败";
                this.Button_Save.Enabled=false;
            }
        }
    }
}
